package com.study.web;

import com.study.bean.Ds;
import com.study.bean.Pic;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.*;

/**
 * swaggerUI
 */
@Slf4j
@Controller
@EnableConfigurationProperties//使用 @EnableConfigurationProperties 开启 @ConfigurationProperties 注解。
@Api(tags = "测试管理相关接口")
public class HelloController {
    /**
     * swagger 测试|https://blog.csdn.net/qq_36249132/article/details/90109815
     * https://blog.csdn.net/u012702547/article/details/88775298 |如果集成了 Security 需要放行
     */
    @Autowired
    StringRedisTemplate redisTemplate;

    //@ApiOperation("添加用户的接口")
    //@ApiImplicitParams({
    //        @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
    //        @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
    //}
    @ApiOperation("显示UUID")
    @ApiImplicitParam(name = "is", value = "测试传递参数", defaultValue = "李四")
    @GetMapping("sms")
    public String sendMsm(String is) {
        String s = UUID.randomUUID().toString();
        return s + is;
    }

    @RequestMapping(value = "ds", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Ds getDs() {
        Date date = new Date();
        Ds ds = new Ds();
        ds.setDate(date).setDatetime(date).setTime(date).setTimestamp(date);
        log.info(ds.toString());
        return ds;
    }

    @GetMapping("hello")
    @ResponseBody
    public Map<String, String> hel() {
        Map<String, String> result = new HashMap<>();
        result.put("date", new DateTime().toString());
        return result;
    }

    @GetMapping(value = {"/index", "/"})
    public String hello(ModelMap modelMap, RedirectAttributes redirectAttributes) {
        modelMap.put("hello", "测试");
        modelMap.put("isOk", 1);
        modelMap.addAttribute("name", "学习测试");
        List<String> list = new ArrayList();
        for (int i = 0; i < 5; i++) {
            list.add("数据" + i);
        }
        modelMap.put("list", list);

        //
        BigDecimal bigDecimal = new BigDecimal("51000.5151");
        modelMap.put("num", bigDecimal);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name", "map测试");

        modelMap.put("map", hashMap);

        //重定向携带数据
        redirectAttributes.addFlashAttribute("attr", "重定向取数据");
        return "index";
    }

    @Autowired
    Pic pic;


    @GetMapping(value = {"showpic", "pic"})
    public String showPic() {
        log.info("显示图片");
        return String.format("redirect:%s", pic.showPic());
    }

    @GetMapping("href")
    public String href(ModelMap modelMap, HttpSession session) {

        modelMap.addAttribute("imgSrc", "1.jpg");
        return "href";
    }
}
